package com.loft.controller.renren;

import com.loft.R;
import com.loft.comm.MyApp;
import com.loft.comm.util.LogCenter;
import com.loft.controller.baseview.BaseActivity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @ClassName: RenRenOauthWebView.java
 * @Description:renren oauth2.0登陆֤
 * @Author: heb
 * @CreateDate: 2012/05/13
 */
public class RenRenOauthWebView extends BaseActivity {

	private WebView webView;
	private WebSettings settings;
	private String accessToken;
	private boolean _isShowDialog = false;
	private RotateAnimation _rotateAnimation;
	private Dialog _progressDialog;
	private ImageView _dialog_circle_imageView;
	private TextView _dialog_msg_textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getAccessToken();
	}

	@Override
	public void initView() {
		// LogCenter.getInstance().debugRESULT(_width + " " + _heigth);
		setContentView(R.layout.oauth_page);
		_progressDialog = new Dialog(this, R.style.dialog);
		View v = getLayoutInflater().inflate(R.layout.dialog, null);
		_progressDialog.setContentView(v, new LayoutParams(_width - 100,
				_width - 100));
		_dialog_circle_imageView = (ImageView) v
				.findViewById(R.id.dialog_circle);
		_dialog_msg_textView = (TextView) v.findViewById(R.id.dialog_msg);
		_dialog_msg_textView.setText(R.string.dialog_ouath_msg);
		webView = (WebView) findViewById(R.id.webView);
	}

	@Override
	public void initAnimation() {
		// LogCenter.getInstance().debugRESULT(
		// "_dialog_circle_imageView.getWidth() / 2:"
		// + _dialog_circle_imageView.getWidth() / 2
		// + " _dialog_circle_imageView.getHeight() / 2:"
		// + _dialog_circle_imageView.getHeight() / 2);
		_rotateAnimation = new RotateAnimation(0, -360, 75, 75);
		_rotateAnimation.setDuration(1000);
		_rotateAnimation.setRepeatCount(Animation.INFINITE);
		_rotateAnimation.setRepeatMode(Animation.RESTART);
		_dialog_circle_imageView.startAnimation(_rotateAnimation);
	}

	@Override
	public void setListeners() {
		// TODO Auto-generated method stub
		super.setListeners();
	}

	private void getAccessToken() {

		String access_token_url = MyApp.RENREN_AUTHORIZATIONURL
				+ "?"
				+ "client_id="
				+ MyApp.RENREN_APPKEY
				+ "&response_type=token"
				+ "&scope=read_user_checkin+read_user_feed+read_user_guestbook+read_user_status"
				+ "&redirect_uri=" + MyApp.RENREN_REDIRECTURL
				+ "&display=mobile";

		settings = webView.getSettings();
		settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		settings.setJavaScriptEnabled(true);
		settings.setSupportZoom(true);
		settings.setBuiltInZoomControls(true);
		webView.loadUrl(access_token_url);
		webView.requestFocusFromTouch();
		webView.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				if (!_isShowDialog) {
					_progressDialog.show();
					_isShowDialog = true;
				}
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				// LogCenter.getInstance().debugRESULT(url);
				if (url.contains("login_denied")) {
					RenRenOauthWebView.this.setResult(
							MyApp.ACTIVITY_NAME_RENREN_OAUTH_RESULTCODE,
							getIntent().putExtra("Callback",
									MyApp.RENREN_AUTHORIZATION_CANCELED));
					_progressDialog.dismiss();
					_rotateAnimation.cancel();
					RenRenOauthWebView.this.finish();
				} else {
					if (url.indexOf("access_token") != -1) {
						int startPos = url.indexOf("token=") + 6;
						int endPos = url.indexOf("&expires_in");
						accessToken = url.substring(startPos, endPos);
						accessToken = accessToken.replace("%7C", "|");
						LogCenter.getInstance().debugRESULT(accessToken);
						SharedPreferences sp = getSharedPreferences(
								MyApp.RENREN_SHAREDPREFERENCES,
								Context.MODE_PRIVATE);
						Editor editor = sp.edit();
						editor.putString(
								MyApp.RENREN_SHAREDPREFERENCES_ACCESS_TOKEN,
								accessToken);
						editor.commit();
						RenRenOauthWebView.this.setResult(Activity.RESULT_OK,
								getIntent().putExtra("Callback", accessToken));
						_progressDialog.dismiss();
						_rotateAnimation.cancel();
						finish();
					}
				}
				_progressDialog.hide();
			}

		});

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			_progressDialog.dismiss();
			_rotateAnimation.cancel();
			RenRenOauthWebView.this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
